package de.playmee.tasks;

import de.playmee.enums.Result;
import de.playmee.handler.AccountHandler;

import android.os.AsyncTask;
import android.util.Log;

/**
 * Asynchronous task that registers a new user account. Upon finishing,
 * it notifies the indicated {@AccountRegisteredListener}.
 */
public class RegisterAccountTask extends AsyncTask<Void, Void, Void> {

	private final String TAG = "RegisterAccountListener";
	
	private AccountHandler aHandler;
	private String username;
	private String password;
	private RegisterAccountListener listener;
	private Result result;
	
	public RegisterAccountTask(AccountHandler aHandler, String username, String password,
			RegisterAccountListener listener) {
		this.aHandler = aHandler;
		this.username = username;
		this.password = password;
		this.listener = listener;
	}
	
	@Override
	protected Void doInBackground(Void... params) {
		Log.i(TAG, "Start registering the user account...");
		listener.onStartRegisteringAccount();
		result = aHandler.registerAccount(username, password);
		Log.i(TAG, "Finished registering the user account.");
		return null;
	}
	
	@Override
	protected void onPostExecute(Void result) {
		listener.onAccoutRegistered(this.result, username, password);
	}
	
	public interface RegisterAccountListener {
		public void onStartRegisteringAccount();
		public void onAccoutRegistered(Result result, String username, String password);
	}
}
